/// @tags: Stack BinarySearch
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>

using std::cin, std::cout;

namespace BlueQuantum {

constexpr int maxn = 5e5 + 1;

int a[maxn];
long long ans;
std::vector<int> stk;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    auto pos = std::upper_bound(stk.crbegin(), stk.crend(), a[i]);
    ans += pos == stk.crend() ? pos - stk.crbegin() : pos - stk.crbegin() + 1;
    pos = std::lower_bound(stk.crbegin(), stk.crend(), a[i]);
    stk.resize(stk.size() - (pos - stk.crbegin()));
    stk.push_back(a[i]);
  }
  cout << ans << std::endl;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P1823 [COI2007] Patrik 音乐会的等待.in", "r", stdin);
  freopen("P1823 [COI2007] Patrik 音乐会的等待.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
